#!/usr/bin/env python
# -*- coding: utf-8 -*-
#       qualityclass.py
#
#       Copyright 2011 vvs <Skochko@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.


import os
import csv


#CSV_FILENAME = os.path.join(os.path.dirname(os.path.abspath(dimcalc.__file__)),'data/qualityclass.csv')
CSV_FILENAME = 'qualityclass.csv'

class QualityClass():
    def __init__(self, args):
        '''
        name, min_dim, max_dim, up_tolerance, un_tolerance, hole, shaft, recomended
        '''
        self.name = args[0]
        self.min_dim = float(args[1])
        self.max_dim = float(args[2])
        self.up_tolerance = float(args[3])
        self.un_tolerance = float(args[4])
        #self.hole = args[5]
        #self.shaft = args[6]
        #self.recomended = args[7]
    def __str__(self):
        return self.name+','+str(self.min_dim)+','+str(self.max_dim)+','+str(self.up_tolerance)+','+str(self.un_tolerance)

qualityclass_list = []
qualityclasses = []

reader = csv.reader(open(CSV_FILENAME, "rb"))

for row in reader:
    if not (row[0] in qualityclasses):
        qualityclasses.append(row[0])

    qualityclass_list.append(QualityClass(row))

def get_tolerance(name, value):
    for qualityclass in qualityclass_list:
        if qualityclass.name == name:
            if (value > qualityclass.min_dim) & (value <= qualityclass.max_dim):
                return (qualityclass.up_tolerance, qualityclass.un_tolerance)
    return (0, 0)

qualityclasses = sorted(qualityclasses)
